package robato.ui;


import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

import java.io.File;


public class Grafico extends JPanel implements ActionListener {

	private static final long serialVersionUID = -6383792098731421408L;

	JFreeChart chart;
	ChartPanel chartPanel;
	XYDataset dataset;
	XYSeriesCollection collectionSeries;
	XYSeries serie;
	JFileChooser fc;
	private JButton btnGuardar;

	
	Grafico(String strDirectorio) {
		this.serie = new XYSeries("Aprendizaje");
		this.collectionSeries = new XYSeriesCollection();
		this.collectionSeries.addSeries(serie);
		this.chart = createChart(collectionSeries);
		this.chartPanel = new ChartPanel(chart);
		this.chartPanel.setPreferredSize(new java.awt.Dimension(480, 300));
		
		this.btnGuardar = new JButton("Guardar Imagen");
		this.btnGuardar.addActionListener(this);
		JPanel panel = new JPanel();
		panel.add(btnGuardar);
		
		this.setLayout(new BorderLayout(0, 0));
		this.add(this.chartPanel, BorderLayout.CENTER);
		this.add(panel, BorderLayout.SOUTH);
		
		this.fc = new JFileChooser();
		this.fc.addChoosableFileFilter(new FileNameExtensionFilter("Imagenes", "JGP","PNG"));
		this.fc.setCurrentDirectory(new File(strDirectorio));
	}

	public synchronized void addPoint(double dblX, double dblY) {
		this.serie.add(dblX, dblY);
		this.chart.fireChartChanged();
		//chartPanel.update(chartPanel.getGraphics());
	}
	
	public void clearSeries(){
		this.serie.clear();
	}
	

	private JFreeChart createChart(final XYDataset dataset) {
		final JFreeChart chart = ChartFactory.createXYLineChart("Curva de Aprendizaje", // chart title
				"Iteracion", // x axis label
				"% Aprz", // y axis label
				dataset, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
				);

		return chart;
	}

	public static void main(final String[] deflt) {

		final Grafico demo = new Grafico("c://robots/");
		JFrame frame = new JFrame();
		frame.setContentPane(demo);
		frame.setSize(new Dimension(520, 320));
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		RefineryUtilities.centerFrameOnScreen(frame);
		frame.setVisible(true);
		try {
			for (double x = 0; x < 100; x++) {
				Thread.sleep(100);
				double y = Math.random() * 2;
				demo.addPoint(x, y);
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == this.btnGuardar) {
			this.saveImagen("");
		}
	}

	public void saveImagen(String strFile) {
		if (strFile == null || strFile.trim().equals("")) {
			int returnVal = fc.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File ofile = fc.getSelectedFile();
				try {
					strFile = ofile.getAbsolutePath();
					
					if (!strFile.contains(".png") ){
						strFile+=".png";
					}
					
					File file = new File(strFile);
					ChartUtilities.saveChartAsPNG(file, this.chart, 400, 250);	
				} 
				catch (Exception ex) {
					System.out.println(ex);
				}
			}
		}
	}

}
